package Thread_communication;

public class Account {
    private String carId;
    private double money;

    public Account(String carId, double money) {
        this.carId = carId;
        this.money = money;
    }

    public synchronized void drawMoney(double money) {
        try {
            String name = Thread.currentThread().getName();
            if (this.money >= money){
                this.money -= money;
                System.out.println(name + "取了" + money  + "元，余额为" + this.money + "元");
            }
            else {
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public synchronized void DepositMoney(double money) {
        try {
            String name = Thread.currentThread().getName();
            if (this.money < money){
                this.money += money;
                System.out.println(name + "存了" + money  + "元，余额为" + this.money + "元");
                this.notifyAll();
                this.wait();
            }
            else {
                this.notifyAll();
                this.wait();
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
